package com.tushen.task;


import com.tushen.utils.SqlSessionFactoryUtil;

import java.io.File;

/** 三维流场和浓度场数据处理
 * 分别调用浓度场和流场的处理程序
 * @author likun
 * @version 1.0
 * @date 2020/12/12 9:03 下午
 */
public class UVThread extends Thread{

    String fileDir;
    String tableName;
    String taskid;
    String version;

    int num=0;

    public UVThread(String fileDir, String taskid, String version){
        this.fileDir=fileDir;
        this.tableName="uv_shp_result_"+version;;
        this.taskid=taskid;
        this.version=version;
    }

    public void run(){
        File file=new File(fileDir);
        String fileSep=java.io.File.separator;
        String pathU=fileDir+fileSep+"u.csv";
        String pathV=fileDir+fileSep+"v.csv";
        File fileU=new File(pathU);
        File fileV=new File(pathV);

        int status=(file.exists()&&fileU.exists()&&fileV.exists())?2:0;
        if(status==2){
            Runnable runnable=new Runnable() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    Dye3.processDye3(fileDir,version);
                    num++;
                    if(num==2){
                        String updateTaskSql="update data_process_task set end_dtm=now(),status="+status+" where taskid='"+taskid+"'";
                        SqlSessionFactoryUtil.excuteUpdateSql(updateTaskSql);
                    }
                }
            };
            Thread thread=new Thread(runnable);
            thread.start();
            UV3.processUV(fileDir,version);
        }
        num++;

        String updateVersionSql=
                "with a as (SELECT min(dt) as min_dt,max(dt) as max_dt FROM "+tableName+")\n" +
                ",b as (\n" +
                "SELECT  min(tm) as min_tm  from  "+tableName+" where dt = (SELECT min_dt from a))\n" +
                ",c as (\n" +
                "SELECT  max(tm) as max_tm  from "+tableName+" where dt = (SELECT max_dt from a))\n" +
                "\n" +
                "insert into data_dtm_verion(m_type,m_version,start_dtm,end_dtm) " +
                 "SELECT 'uv3','"+version+"', (min_dt+min_tm)::TIMESTAMP as start_dtm,(max_dt+max_tm)::TIMESTAMP as end_dtm from a,b,c" +
                        "  on  CONFLICT(m_type,m_version) do nothing;";
        String updateVersionSql2="insert into data_dtm_verion(m_type,m_version,start_dtm,end_dtm) " +
                "SELECT 'dye3',m_version, start_dtm, end_dtm from data_dtm_verion where m_version='"+version+"' and m_type='uv3' " +
                "  on  CONFLICT(m_type,m_version) do nothing;";
        if(status==2){
            SqlSessionFactoryUtil.excuteUpdateSql(updateVersionSql);
            SqlSessionFactoryUtil.excuteUpdateSql(updateVersionSql2);
        }

        if(num==2||status==0){
            String updateTaskSql="update data_process_task set end_dtm=now(),status="+status+" where taskid='"+taskid+"'";
            SqlSessionFactoryUtil.excuteUpdateSql(updateTaskSql);
        }

    }
}
